﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Hook.Graphics;

namespace Hook.Primitives
{
    public class TextField
    {
        public TextSprite Sprite;
        public string Text;
        public bool Selected;
        public Vector3 Position;
        public Color TextColor;

        public bool[] LetterKeyDown = new bool[26];
        public bool[] NumberKeyDown = new bool[10];
        public bool BackKeyDown;
        public bool PeriodKeyDown;

        public TextField(Vector3 Position)
        {
            Text = "";
            Selected = false;
            TextColor = Color.Black;
            Sprite = new TextSprite(Text);
            this.Position = Position;
            Sprite.Position = Position;
        }

        public void Update(GameTime gt)
        {
            HandleInput();

            if (Selected)
            {
                TextColor = Color.DarkRed;
            }
            else
            {
                TextColor = Color.Black;
            }

            Sprite.Text = Text;
            Sprite.Color = TextColor;
            Sprite.Position = Position;
            Sprite.Update(gt);
        }

        public void Draw(SpriteBatch sb)
        {
            Sprite.Draw(sb);
        }

        public void HandleInput()
        {
            KeyboardState kbs = Keyboard.GetState();
            MouseState ms = Mouse.GetState();

            if (Selected)
            {
                char Letter;
                int Number;

                if (kbs.IsKeyDown(Keys.Back) && !BackKeyDown)
                {
                    if (Text.Length > 0)
                    {
                        Text = Text.Remove(Text.Length - 1);
                    }
                    BackKeyDown = true;
                }

                if (kbs.IsKeyDown(Keys.OemPeriod) && !PeriodKeyDown)
                {
                    Text += '.';
                    PeriodKeyDown = true;
                }

                if (kbs.IsKeyDown(Keys.LeftShift) || kbs.IsKeyDown(Keys.RightShift))
                {
                    Letter = 'A';
                    if (kbs.IsKeyDown(Keys.A) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'B';
                    if (kbs.IsKeyDown(Keys.B) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'C';
                    if (kbs.IsKeyDown(Keys.C) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'D';
                    if (kbs.IsKeyDown(Keys.D) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'E';
                    if (kbs.IsKeyDown(Keys.E) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'F';
                    if (kbs.IsKeyDown(Keys.F) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'G';
                    if (kbs.IsKeyDown(Keys.G) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'H';
                    if (kbs.IsKeyDown(Keys.H) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'I';
                    if (kbs.IsKeyDown(Keys.I) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'J';
                    if (kbs.IsKeyDown(Keys.J) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'K';
                    if (kbs.IsKeyDown(Keys.K) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'L';
                    if (kbs.IsKeyDown(Keys.L) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'M';
                    if (kbs.IsKeyDown(Keys.M) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'N';
                    if (kbs.IsKeyDown(Keys.N) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'O';
                    if (kbs.IsKeyDown(Keys.O) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'P';
                    if (kbs.IsKeyDown(Keys.P) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'Q';
                    if (kbs.IsKeyDown(Keys.Q) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'R';
                    if (kbs.IsKeyDown(Keys.R) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'S';
                    if (kbs.IsKeyDown(Keys.S) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'T';
                    if (kbs.IsKeyDown(Keys.T) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'U';
                    if (kbs.IsKeyDown(Keys.U) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'V';
                    if (kbs.IsKeyDown(Keys.V) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'W';
                    if (kbs.IsKeyDown(Keys.W) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'X';
                    if (kbs.IsKeyDown(Keys.X) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'Y';
                    if (kbs.IsKeyDown(Keys.Y) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                    Letter = 'Z';
                    if (kbs.IsKeyDown(Keys.Z) && !LetterKeyDown[Letter - 'A'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'A'] = true;
                    }
                }
                else if (kbs.IsKeyUp(Keys.LeftShift) && kbs.IsKeyUp(Keys.RightShift))
                {
                    Letter = 'a';
                    if (kbs.IsKeyDown(Keys.A) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'b';
                    if (kbs.IsKeyDown(Keys.B) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'c';
                    if (kbs.IsKeyDown(Keys.C) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'd';
                    if (kbs.IsKeyDown(Keys.D) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'e';
                    if (kbs.IsKeyDown(Keys.E) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'f';
                    if (kbs.IsKeyDown(Keys.F) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'g';
                    if (kbs.IsKeyDown(Keys.G) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'h';
                    if (kbs.IsKeyDown(Keys.H) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'i';
                    if (kbs.IsKeyDown(Keys.I) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'j';
                    if (kbs.IsKeyDown(Keys.J) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'k';
                    if (kbs.IsKeyDown(Keys.K) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'l';
                    if (kbs.IsKeyDown(Keys.L) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'm';
                    if (kbs.IsKeyDown(Keys.M) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'n';
                    if (kbs.IsKeyDown(Keys.N) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'o';
                    if (kbs.IsKeyDown(Keys.O) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'p';
                    if (kbs.IsKeyDown(Keys.P) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'q';
                    if (kbs.IsKeyDown(Keys.Q) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'r';
                    if (kbs.IsKeyDown(Keys.R) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 's';
                    if (kbs.IsKeyDown(Keys.S) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 't';
                    if (kbs.IsKeyDown(Keys.T) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'u';
                    if (kbs.IsKeyDown(Keys.U) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'v';
                    if (kbs.IsKeyDown(Keys.V) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'w';
                    if (kbs.IsKeyDown(Keys.W) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'x';
                    if (kbs.IsKeyDown(Keys.X) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'y';
                    if (kbs.IsKeyDown(Keys.Y) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                    Letter = 'z';
                    if (kbs.IsKeyDown(Keys.Z) && !LetterKeyDown[Letter - 'a'])
                    {
                        Text += Letter;
                        LetterKeyDown[Letter - 'a'] = true;
                    }
                }

                Number = 0;
                if (kbs.IsKeyDown(Keys.D0) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 1;
                if (kbs.IsKeyDown(Keys.D1) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 2;
                if (kbs.IsKeyDown(Keys.D2) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 3;
                if (kbs.IsKeyDown(Keys.D3) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 4;
                if (kbs.IsKeyDown(Keys.D4) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 5;
                if (kbs.IsKeyDown(Keys.D5) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 6;
                if (kbs.IsKeyDown(Keys.D6) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 7;
                if (kbs.IsKeyDown(Keys.D7) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 8;
                if (kbs.IsKeyDown(Keys.D8) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }
                Number = 9;
                if (kbs.IsKeyDown(Keys.D9) && !NumberKeyDown[Number])
                {
                    Text += Number.ToString();
                    NumberKeyDown[Number] = true;
                }

                if (kbs.IsKeyUp(Keys.Back) && BackKeyDown)
                {
                    BackKeyDown = false;
                }
                if (kbs.IsKeyUp(Keys.OemPeriod) && PeriodKeyDown)
                {
                    PeriodKeyDown = false;
                }

                if (kbs.IsKeyUp(Keys.A))
                {
                    LetterKeyDown['A' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.B))
                {
                    LetterKeyDown['B' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.C))
                {
                    LetterKeyDown['C' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.D))
                {
                    LetterKeyDown['D' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.E))
                {
                    LetterKeyDown['E' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.F))
                {
                    LetterKeyDown['F' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.G))
                {
                    LetterKeyDown['G' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.H))
                {
                    LetterKeyDown['H' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.I))
                {
                    LetterKeyDown['I' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.J))
                {
                    LetterKeyDown['J' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.K))
                {
                    LetterKeyDown['K' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.L))
                {
                    LetterKeyDown['L' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.M))
                {
                    LetterKeyDown['M' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.N))
                {
                    LetterKeyDown['N' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.O))
                {
                    LetterKeyDown['O' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.P))
                {
                    LetterKeyDown['P' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.Q))
                {
                    LetterKeyDown['Q' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.R))
                {
                    LetterKeyDown['R' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.S))
                {
                    LetterKeyDown['S' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.T))
                {
                    LetterKeyDown['T' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.U))
                {
                    LetterKeyDown['U' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.V))
                {
                    LetterKeyDown['V' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.W))
                {
                    LetterKeyDown['W' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.X))
                {
                    LetterKeyDown['X' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.Y))
                {
                    LetterKeyDown['Y' - 'A'] = false;
                }
                if (kbs.IsKeyUp(Keys.Z))
                {
                    LetterKeyDown['Z' - 'A'] = false;
                }

                if (kbs.IsKeyUp(Keys.D0))
                {
                    NumberKeyDown[0] = false;
                }
                if (kbs.IsKeyUp(Keys.D1))
                {
                    NumberKeyDown[1] = false;
                }
                if (kbs.IsKeyUp(Keys.D2))
                {
                    NumberKeyDown[2] = false;
                }
                if (kbs.IsKeyUp(Keys.D3))
                {
                    NumberKeyDown[3] = false;
                }
                if (kbs.IsKeyUp(Keys.D4))
                {
                    NumberKeyDown[4] = false;
                }
                if (kbs.IsKeyUp(Keys.D5))
                {
                    NumberKeyDown[5] = false;
                }
                if (kbs.IsKeyUp(Keys.D6))
                {
                    NumberKeyDown[6] = false;
                }
                if (kbs.IsKeyUp(Keys.D7))
                {
                    NumberKeyDown[7] = false;
                }
                if (kbs.IsKeyUp(Keys.D8))
                {
                    NumberKeyDown[8] = false;
                }
                if (kbs.IsKeyUp(Keys.D9))
                {
                    NumberKeyDown[9] = false;
                }
            }
        }
    }
}
